#include "Exception.h"
#include <cstdlib>
#include <cstring>
NAME_SPACE_BEGIN


Exception::Exception(const char* message)
{
	init(message, NULL, 0);
}

Exception::Exception(const char* file, int line)
{
	init(NULL, file, line);
}

Exception::Exception(const char* message, const char* file, int line)
{
	init(message, file, line);
}

Exception::Exception(const Exception& e)
{
	m_message = _strdup(e.m_message);
	m_location = _strdup(e.m_location);
}

Exception& Exception::operator= (const Exception& e)
{
	if (this != &e)
	{
		free(m_message);
		free(m_location);

		m_message = _strdup(e.m_message);
		m_location = _strdup(e.m_location);
	}

	return *this;
}

const char* Exception::message() const
{
	return m_message;
}
const char* Exception::location() const
{
	return m_location;
}

Exception::~Exception()
{
	free(m_message);
	free(m_location);
}

void Exception::init(const char* message, const char* file, int line)
{
	m_message = _strdup(message);
	if (nullptr != file)
	{
		char sl[16] = { 0 };
		_itoa(line, sl, 10);
		m_location = static_cast<char*>(malloc(strlen(file) + strlen(sl) + 2));
		m_location = strcpy(m_location, file);
		m_location = strcat(m_location, ":");
		m_location = strcat(m_location, sl);
	}
	else {
		m_location = nullptr;
	}
	
}



NAME_SPACE_END